package com.wbao.dianniu.ui;


import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.qcloud.timchat.utils.LoginBusiness;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.customView.NoDoubleClickListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.SharedPreferencesUtils;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.update.UpdateManager;
import com.wbao.dianniu.update.UpdateManager.ICheckCompletedListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.PermissionUtils;
import com.wbao.dianniu.utils.Utils;

/**
 * 设置
 *
 * @author yejianjun
 */
public class SettingActivity extends BaseActivity implements ICheckCompletedListener {

    private final String TAG = SettingActivity.class.getSimpleName();
    public RelativeLayout rl_account, /*rl_privacy,*//* rl_base, */rl_about, rl_exist, rl_self_agreement, rl_user_agreement, rl_privacy_agreement;
    private RelativeLayout rl_feedback;
    private RelativeLayout rl_checkUpdate;
    //    private RelativeLayout rl_invite;
    private RelativeLayout rl_share;
    private Dialog pDialog;
    private UpdateManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_setting, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.setting));
        init();
        manager = new UpdateManager(this);
        manager.addCheckListener(this);
    }

    private void init() {
        rl_account = findViewById(R.id.rl_account);
//        rl_privacy = findViewById(R.id.rl_privacy);
//        rl_base = findViewById(R.id.rl_base);
        rl_about = findViewById(R.id.rl_about);
        rl_feedback = findViewById(R.id.rl_feedback);
        rl_exist = findViewById(R.id.rl_exist);
        rl_checkUpdate = findViewById(R.id.rl_check_update);
        rl_share = findViewById(R.id.rl_share_to_friends);
        rl_self_agreement = findViewById(R.id.rl_self_discipline);
        rl_user_agreement = findViewById(R.id.rl_user_agreement);
        rl_privacy_agreement = findViewById(R.id.rl_privacy_agreement);

        rl_account.setOnClickListener(noDoubleClickListener);
//        rl_privacy.setOnClickListener(noDoubleClickListener);
//        rl_base.setOnClickListener(noDoubleClickListener);
        rl_about.setOnClickListener(noDoubleClickListener);
        rl_feedback.setOnClickListener(noDoubleClickListener);
        rl_exist.setOnClickListener(noDoubleClickListener);
        rl_checkUpdate.setOnClickListener(noDoubleClickListener);
        rl_share.setOnClickListener(noDoubleClickListener);
        rl_self_agreement.setOnClickListener(noDoubleClickListener);
        rl_user_agreement.setOnClickListener(noDoubleClickListener);
        rl_privacy_agreement.setOnClickListener(noDoubleClickListener);

    }

    private void showWaittingDialog() {
        manager.checkUpdate();
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeCheckListener();
        }
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }

    }

    private void intent(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(SettingActivity.this, cls);
        startActivity(intent);
    }

    @Override
    public void checkCompleted() {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    @Override
    public void noCheck() {
        Notification.toast(this, "当前版本已是最新版本");
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    @Override
    public void noPermission() {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        PermissionUtils.getInstance().showMsg(this, "更新失败，没有读取权限，\n请点击\"设置\"-\"权限管理\"-打开所有权限");
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.rl_account:
                    intent(AccountInfoActivity.class);
                    break;
                /*case R.id.rl_privacy:
                    intent(PrivacySettingActivity.class);
                    break;*/
                /*case R.id.rl_base:
                    intent(BaseSetting.class);
                    break;*/
                case R.id.rl_about:
                    Intent aboutIntent = new Intent();
                    aboutIntent.setClass(SettingActivity.this, AboutActivity.class);
                    startActivity(aboutIntent);
                    break;
                case R.id.rl_check_update:
                    showWaittingDialog();
                    break;
                /*case R.id.rl_invite_friends:// 邀请好友加入电纽
                    PermissionUtils.getInstance().intentToContact(SettingActivity.this);
                    break;*/
                case R.id.rl_share_to_friends:// 分享给好友
                    ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                    shareSDKManager.showShare(SettingActivity.this, getResources().getString(R.string.app_share_content), 3, GlobalContext.cdndownUrl + "logo.png", GlobalContext.getAccountId());
                    break;
                case R.id.rl_feedback:
                    Intent feedIntent = new Intent();
                    feedIntent.setClass(SettingActivity.this, FeedbackActivity.class);
                    startActivity(feedIntent);
                    break;
                case R.id.rl_exist:
                    CustomDialog.Builder builder = new CustomDialog.Builder(SettingActivity.this);
                    builder.setTitle("确认退出吗？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            loginOut();

                        }
                    });
                    builder.setNegativeButton("取消",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    builder.create().show();
                    break;
                case R.id.rl_self_discipline:
                    Intent selfIntent = new Intent();
                    selfIntent.setClass(SettingActivity.this, SelfConventionActivity.class);
                    startActivity(selfIntent);
                    break;
                case R.id.rl_user_agreement:
                    Intent userIntent = new Intent();
                    userIntent.setClass(SettingActivity.this, UserAgreementActivity.class);
                    startActivity(userIntent);
                    break;
                case R.id.rl_privacy_agreement:
                    Intent privacyIntent = new Intent();
                    privacyIntent.setClass(SettingActivity.this, PrivacyAgreementActivity.class);
                    startActivity(privacyIntent);
                    break;
                default:
                    break;
            }
        }
    };

    private void loginOut() {
        boolean net = Utils.isNetworkAvaiable(SettingActivity.this);
        if (!net) {
            Notification.toast(SettingActivity.this, "网络不可用，请检测网络");
            // TODO Auto-generated method stub
            return;
        }

        // 显示退出等待框
        pDialog = LoadingWaitDialog.createLoadingDialog(this, getResources().getString(R.string.existing));
//        pDialog.setCancelable(false);
        pDialog.show();

        LoginBusiness.logout(new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                if (null != pDialog && pDialog.isShowing()) {
                    pDialog.dismiss();
                }
                if (i == 6013) {
                    successProcess();
                } else {
                    Notification.toast(SettingActivity.this, getResources().getString(R.string.setting_logout_fail));
                }
                Log.e("tenIM","login out error code :"+i +" message :"+s);
            }

            @Override
            public void onSuccess() {
                Log.e("tenIM","login out  success ");
                if (null != pDialog && pDialog.isShowing()) {
                    pDialog.dismiss();
                }
                successProcess();
            }
        });

    }

    private void successProcess() {
        SharedPreferencesUtils.init(SettingActivity.this);
        SharedPreferencesUtils.setAutoLoginStatus(SettingActivity.this, false);
        GlobalContext.cleanCacheToken();
        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        overridePendingTransition(0, R.anim.push_bottom_out);
        SettingActivity.this.finish();
    }


}
